package com.wzh.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzh.springboot.VO.ResultVO;
import com.wzh.springboot.config.FastDFSUtils;

import com.wzh.springboot.entity.Img;
import com.wzh.springboot.entity.Spectrum;
import com.wzh.springboot.service.ImgService;
import com.wzh.springboot.service.SpectrumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Spectrum)表控制层
 *
 * @author makejava
 * @since 2020-05-04 01:02:37
 */
@RestController
@RequestMapping("spectrum")
public class SpectrumController extends ApiController {

    @Autowired
    private SpectrumService spectrumService;

    @Autowired
    private ImgService imgService;

    //    /**
//     *  分页查询所有数据  带具体参数
//     * @param current  当前页
//     * @param size 每页显示大小
//     * @return
//     */
    @GetMapping("/page/{current}/{size}/{category}/{subcategory}")
    public ResultVO findAll(@PathVariable("current") Integer current,
                            @PathVariable("size") Integer size,
                            @PathVariable("category") Integer category,
                            @PathVariable("subcategory") Integer subcategory) {
        QueryWrapper<Spectrum> wrapper = new QueryWrapper();
        wrapper.eq("category",category).eq("subcategory",subcategory);
        Page<Spectrum> page = new Page<>(current, size);
        Page<Spectrum> spectrumPage = spectrumService.page(page, wrapper);
        return ResultVO.success.code(100).msg("正在查询第" + current + "页\n当前一页" + size + "条数据").data(spectrumPage);
    }

    /**
     * 分页查询所有数据  第二种以分页对象
     *
     * @param page 分页对
     * @return 所有数据
     */
    @GetMapping("/page")
    public ResultVO selectAll(Page<Spectrum> page) {
        return ResultVO
                .fail
                .code(100)
                .msg("正在查询第" + page + "页\n当前一页" + page.getSize() + "条数据")
                .data(this.spectrumService.page(page, Wrappers.emptyWrapper()));
    }


    /**
     * 搜索框条件查询
     *
     * @param spectrum 查询实体
     * @return
     */
  @PostMapping("/findBy")                       //以json接收数据并转换成实体类
    public ResultVO findBy(Page<Spectrum> page, @RequestBody  Spectrum spectrum) {
        QueryWrapper<Spectrum> wrapper = new QueryWrapper<>();
        wrapper
                .like("spectrumName", spectrum.getSpectrumName())
                .or()
                .like("author", spectrum.getAuthor());
        Page<Spectrum> resultPage = this.spectrumService.page(page, wrapper);
        if (resultPage.getRecords().isEmpty()){
            return ResultVO
                    .fail
                    .code(-100)
                    .msg("没找到歌曲名为"+spectrum.getSpectrumName()
                            +"//作者为"+spectrum.getAuthor()+"的曲谱")
                    .data(null);
        }else {
            return ResultVO
                    .success
                    .code(100)
                    .msg("查询成功\n歌曲名为"+spectrum.getSpectrumName()
                            +"//作者为"+spectrum.getAuthor()+"的曲谱")
                    .data(resultPage);
        }

    }
//    @PostMapping("/findBy/{current}/{size}")
//    public ResultVO findBy(@PathVariable("current") Integer current,
//                           @PathVariable("size") Integer size, @RequestBody(required=false) Spectrum spectrum) {
//        QueryWrapper<Spectrum> wrapper = new QueryWrapper<>();
//        wrapper
//                .like("spectrumName", spectrum.getSpectrumName())
//                .or()
//                .like("author", spectrum.getAuthor());
//        Page<Spectrum> page = new Page<>(current, size);
//        Page<Spectrum> resultPage = this.spectrumService.page(page, wrapper);
//
//            return ResultVO
//                    .success
//                    .code(100)
//                    .msg("查询成功\n歌曲名为"+spectrum.getSpectrumName()
//                            +"//作者为"+spectrum.getAuthor()+"的曲谱")
//                    .data(resultPage);
//
//
//    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/select/{id}")
    public ResultVO selectOne(@PathVariable Serializable id) {
        return ResultVO.success.code(100).msg("查询成功").data(this.spectrumService.getById(id));
    }


    /**
     * 新增数据
     *
     * @param spectrum 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ResultVO insert(@RequestBody Spectrum spectrum) {
        return ResultVO
                .success
                .code(100)
                .msg("新增成功")
                .data(this.spectrumService.save(spectrum));
    }

    /**
     * 修改数据
     *
     * @param spectrum 实体对象
     * @return 修改结果
     */
    @PutMapping
    public ResultVO update(@RequestBody Spectrum spectrum) {
        return ResultVO
                .success
                .code(100)
                .msg("修改成功")
                .data(this.spectrumService.updateById(spectrum));

    }

    /**
     * 删除数据
     *
     * @param spectrumId 主键
     * @return 删除结果
     */
    @DeleteMapping("/delete/{spectrumId}")
    public ResultVO delete(@PathVariable("spectrumId") int spectrumId) {
        return ResultVO
                .success
                .code(100)
                .msg("删除成功")
                .data(this.spectrumService.removeById(spectrumId));
    }


    @Value("${fastdfs.nginx.host}")
    String nginxHost;

    @PostMapping("/img")
    public ResultVO insertIMG(MultipartFile file, Integer spectrumId) {
        String fileId = FastDFSUtils.upload(file);
        String url = nginxHost + fileId;
        Integer integer = imgService.insertIMG(url, spectrumId);
        if (integer == 1) {
            return ResultVO
                    .success
                    .code(100)
                    .msg("图片上传成功")
                    .data(url);
        }
        return ResultVO.fail.code(-100).msg("上传失败").data(null);
    }


    //    通过当前行的id获取当前行的图片路径
    @GetMapping("/img/{spectrumId}")
    public ResultVO getURLById(@PathVariable("spectrumId") Serializable spectrumId) {
        QueryWrapper<Img> wrapper = new QueryWrapper<>();
        wrapper.select("url").eq("spectrumId", spectrumId);
        List<Img> imgList = this.imgService.list(wrapper);
        List<String> urls = imgList.stream().map(Img::getUrl).collect(Collectors.toList());
        return ResultVO.success.code(100).msg("查询成功").data(urls);
    }


    /**
     *   1.先删除
     *   2.增加
     * @param file 更新的文件
     * @param spectrumId
     * @return
     */
    /**
     * @PostMapping("/updateImg") public ResultVO deleteUrlBySpectrumImg(MultipartFile file, Integer spectrumId){
     * String fileId = FastDFSUtils.upload(file);
     * String url = nginxHost + fileId;
     * UpdateWrapper<Img> ew = new UpdateWrapper<>();
     * ew.set("url",url).eq("spectrumId",spectrumId);
     * return ResultVO.success.code(100).msg("更新成功").data(this.imgService.update(ew));
     * }
     **/
    @PostMapping("/updateImg")
    public ResultVO deleteUrlBySpectrumImg(MultipartFile file, int spectrumId) {
        String fileId = FastDFSUtils.upload(file);
        String url = nginxHost + fileId;
        QueryWrapper<Img> wrapper = new QueryWrapper<>();
        wrapper.eq("spectrumId", spectrumId);
        try {
            this.imgService.remove(wrapper);
            this.insertIMG(file, spectrumId);
            return ResultVO.success.code(100).msg("更新成功").data(url);
        } catch (Exception e) {
            return ResultVO.fail.code(-1).msg("运行出错").data(null);
        }
    }


}